<?php
namespace App\Utils\Logic\Ly;
use App\Utils\Service\Ly\SendCode;
use Illuminate\Support\Facades\Cache;

class Logingic
{

    /**
     * 手机号登录
     * @param $phone
     * @param $code
     * @throws \Exception
     */
    public static function  login($phone,$code)
    {
        if(empty($phone)) throw new \Exception('手机号不能为空');
        if(empty($code)) throw new \Exception('验证码不能为空');
        $sendCode=$code.'_'.$phone;
        $sendPhone='phone'.$phone;
        if(!isset($sendPhone)) throw new \Exception('手机号还未发送验证码');
        $getCode=Cache::get($sendCode);
        if($code!=$getCode) throw new \Exception('验证码错误');
        return $code;
    }

    public static function send($phone)
    {
        if(empty($phone)) throw new \Exception('手机号不能为空');
        $code=rand(999,9999);
        $content='验证码是'.$code;
        $result=SendCode::sendSms($content,$code,true);
        if($result['status']!=0) throw new \Exception('短信验证码发送失败');
        $sendCode=$code.'_'.$phone;
        $sendPhone='phone'.$phone;
        if($result['status']==0)
        {
            Cache::set($sendCode,$code,600);
            Cache::set($sendPhone,$phone,600);
        }
        return $code;
    }
}
